package se.webbzon.boltzmann.game.environment;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.environment.EnvironmentInflator;
import se.webbzon.boltzmann.game.region.RegionObject;
import se.webbzon.oschi01.worldeditor.WorldObjectDeflation;
import se.webbzon.oschi01.worldobject.WorldObject;

public abstract class RegionObjectInflator extends EnvironmentInflator {
	
	// The persistences of this inflator
	private boolean gamePersistent, regionPersistent;

	/*============================================================
	Constructors
	============================================================*/
	
	public RegionObjectInflator(AbstractEnvironment env, String worldObjectName) {
		super(env, worldObjectName);
		gamePersistent = false;
		regionPersistent = false;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns true if this inflator is game persistent. **/
	public boolean isGamePersistent() {
		return gamePersistent;
	}

	/** Sets whatever this inflator is game persistent. **/
	public void setGamePersistent(boolean gamePersistent) {
		this.gamePersistent = gamePersistent;
	}

	/** Returns true if this inflator is region persistent. **/
	public boolean isRegionPersistent() {
		return regionPersistent;
	}

	/** Sets whatever this inflator is region persistent. **/
	public void setRegionPersistent(boolean regionPersistent) {
		this.regionPersistent = regionPersistent;
	}
	
	/** Creates a new region object by using the provided serializable 
	 * form of the world object (WorldObjectDeflation). **/ 
	public abstract RegionObject makeRegionObject(WorldObjectDeflation deflation);

	@Override public WorldObject makeWorldObject(WorldObjectDeflation deflation) {
		RegionObject instance = makeRegionObject(deflation);
		if (hasPersistence(getEnvironment(),instance) || !(instance instanceof WorldObject))
			return new RegionObjectImpl(instance);
		else
			return (WorldObject) instance;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Returns true if the deflated instnace has any persistences. **/
	private boolean hasPersistence(AbstractEnvironment env, RegionObject instance) {
		if (env.isDebugMode())
			return false;
		else if (env instanceof GameEnvironment) {
			GameEnvironment genv = (GameEnvironment) env;
			return (genv.hasRegionPersistence(instance) && regionPersistent) ||
					(genv.hasGamePersistence(instance) && gamePersistent);
		} else
			return false;
	}

}
